import { timeFormat, isDate } from "../../libs/utils";
export type Days = {
	id: number;
	date: Date;
	isToday: boolean;
	otherMonth: "nextMonth" | "preMonth" | "nowMonth";
	markClassName?: string;
	isChoose?: boolean;
	dayHide?: boolean;
	noClick?:boolean
	isNowDay?: boolean;
};

type Util = {
	getDaysInOneMonth(date: Date):number,
	getMonthweek(date: Date):number,
	getOtherMonth(date: Date, str:"nextMonth"|"preMonth"):Date,
	getLeftArr(date: Date):Days[],
	getRightArr(date: Date):Days[],
	dateFormat(date: Date | string | number):string,
	getMonthListNoOther(date: Date):Days[],
	getMonthList(date: Date):Days[],
	compareDate(dateA: Date, dateB: Date):0|1|2,
	sundayStart:boolean
}

const timeUtil:Util = {
	// 当某月的天数
	getDaysInOneMonth(date) {
		const year = date.getFullYear();
		const month = date.getMonth() + 1;
		const d = new Date(year, month, 0);
		return d.getDate();
	},
	// 向前空几个
	getMonthweek(date) {
		const year = date.getFullYear();
		const month = date.getMonth() + 1;
		const dateFirstOne = new Date(year + "/" + month + "/1");
		return this.sundayStart
		  ? dateFirstOne.getDay() == 0
		    ? 7
		    : dateFirstOne.getDay()
		  : dateFirstOne.getDay() == 0
		  ? 6
		  : dateFirstOne.getDay() - 1;
	},
	/**
	 * 获取当前日期上个月或者下个月
	 */
	getOtherMonth(date, str) {
		const timeArray = this.dateFormat(date).split("/");
		const year = timeArray[0];
		const month = timeArray[1];
		const day = timeArray[2];
		let year2 = year;
		let month2:number|string;
		if (str === "nextMonth") {
			month2 = parseInt(month) + 1;
			if (month2 == 13) {
				year2 = `${parseInt(year2) + 1}`;
				month2 = 1;
			}
		} else {
			month2 = parseInt(month) - 1;
			if (month2 == 0) {
				year2 = `${parseInt(year2) - 1}`;
				month2 = 12;
			}
		}
		let day2 = day;
		const days2 = new Date(Number(year2), month2, 0).getDate();
		if (Number(day2) > days2) {
			day2 = `${days2}`;
		}
		if (month2 < 10) {
			month2 = "0" + month2;
		}
		if (Number(day2) < 10) {
			day2 = "0" + day2;
		}
		const t2 = year2 + "/" + month2 + "/" + day2;
		return new Date(t2);
	},
	// 上个月末尾的一些日期
	getLeftArr(date) {
		const arr: Days[] = [];
		const leftNum = this.getMonthweek(date);
		const num =
			this.getDaysInOneMonth(this.getOtherMonth(date, "preMonth")) -
			leftNum +
			1;
		const preDate = this.getOtherMonth(date, "preMonth");
		// 上个月多少开始
		for (let i = 0; i < leftNum; i++) {
			const nowTime =
				preDate.getFullYear() +
				"/" +
				(preDate.getMonth() + 1) +
				"/" +
				(num + i);
			arr.push({
				id: num + i,
				date: new Date(nowTime),
				isToday: false,
				otherMonth: "preMonth",
			});
		}
		return arr;
	},
	// 下个月末尾的一些日期
	getRightArr(date) {
		const arr: Days[] = [];
		const nextDate = this.getOtherMonth(date, "nextMonth");
		const leftLength = this.getDaysInOneMonth(date) + this.getMonthweek(date);
		const _length = 7 - (leftLength % 7);
		// const preDate = this.getOtherMonth(date, "preMonth");
		for (let i = 0; i < _length; i++) {
			const nowTime =
				nextDate.getFullYear() +
				"/" +
				(nextDate.getMonth() + 1) +
				"/" +
				(i + 1);
			arr.push({
				id: i + 1,
				date: new Date(nowTime),
				isToday: false,
				otherMonth: "nextMonth",
			});
		}
		return arr;
	},
	// format日期
	dateFormat(date) {
		date = isDate(date) ? date : new Date(date);
		return timeFormat(date.getTime());
	},
	// 获取某月的列表不包括上月和下月
	getMonthListNoOther(date) {
		const arr: Days[] = [];
		const num = this.getDaysInOneMonth(date);
		const year = date.getFullYear();
		const month = date.getMonth() + 1;
		const toDay = this.dateFormat(new Date());

		for (let i = 0; i < num; i++) {
			const nowTime = year + "/" + month + "/" + (i + 1);
			arr.push({
				id: i + 1,
				date: new Date(nowTime),
				isToday: toDay === nowTime,
				otherMonth: "nowMonth",
			});
		}
		return arr;
	},
	// 获取某月的列表 用于渲染
	getMonthList(date) {
		return [
			...this.getLeftArr(date),
			...this.getMonthListNoOther(date),
			...this.getRightArr(date),
		];
	},
	// 比较两个日期
	// 0 dateA小于dateB
	// 1 dateA大于dateB
	// 2 dateA等于dateB
	compareDate(dateA, dateB) {
		const yearA = dateA.getFullYear();
		const yearB = dateB.getFullYear();
		if (yearA === yearB) {
			const monthA = dateA.getMonth();
			const monthB = dateB.getMonth();
			if (monthA === monthB) {
				const dayA = dateA.getDate();
				const dayB = dateB.getDate();
				if (dayA === dayB) return 2;
				return dayA > dayB ? 1 : 0;
			}

			return monthA > monthB ? 1 : 0;
		}

		return yearA > yearB ? 1 : 0;
	},
	// 默认是周一开始
	sundayStart: false,

}

export default timeUtil;
